/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import Cache from './cache'

let count =2000;
let countmax=50000;
let count1=5000;
function put_test() {
    let cache = new Cache()
    let start = Date.now();
    for (let i = 0; i < count1; i++) {
        cache.put('key1', 'this is function', 1, function () { });
    }
    let end = Date.now();
    cache.put('key2', 'value2', 1000);
    cache.put('key3', 'value1');
    let exportedJson = cache.exportJson();
    print(`memory-cache_put: ${end - start} ms`);
    // print(exportedJson,typeof(exportedJson))
    if (exportedJson.indexOf('this is function') == -1) {
        throw new Error('memory-cache_put err')
    }
}
//删除实际只执行一次，耗时太短
function del_test() {
    let cache = new Cache()
    cache.put('key1', 'this is function', 1, function () { });
    cache.put('key2', 'value2', 1000);
    cache.put('key3', 'value1');
    let start = Date.now();
    for (let i = 0; i < count1; i++) {
        cache.del('key1')
    }
    let end = Date.now();
    var exported = cache.exportJson();
    // print(cache.size(),exported)
    print(`memory-cache_del: ${end - start} ms`)
    if (exported.indexOf('key1') != -1) {
        throw new Error('memory-cache_del err')
    }
}

function delAddPut_test() {
    let cache = new Cache()
    cache.put('key2', 'value2', 1000);
    cache.put('key3', 'value1');
    let start = Date.now();
    for (let i = 0; i < count1; i++) {
        cache.put('key1', 'this is function', 20, function () { });
        cache.del('key1')
    }
    let end = Date.now();
    var exported = cache.exportJson();
    // print(cache.size(),exported)
    print(`memory-cache_delAddPut: ${end - start} ms`)
    if (exported.indexOf('key1') != -1) {
        throw new Error('memory-cache_delAddPut err')
    }
}
function size_test() {
    let cache = new Cache()
    let size;
    for(let i=0;i<1000;i++){
        cache.put('key'+i, 'this is function', 100, function () { });
    }
    // cache.put('key1', 'this is function', 100, function () { });
    // cache.put('key2', 'value2', 1000);
    // cache.put('key3', 'value1');
    let start = Date.now();
    for (let i = 0; i < 500000; i++) {
        size = cache.size();
    }
    let end = Date.now();
    print(`memory-cache_size: ${end - start} ms`);
    // print(size)
    if (size != 1000) {
        throw new Error('memory-cache_size err');
    }
}
function memsize_test() {
    let cache = new Cache()
    let size;
    for(let i=0;i<10;i++){
        cache.put('key'+i, 'this is function', 100, function () { });
    }
    // cache.put('key1', 'this is function', 100, function () { });
    // cache.put('key2', 'value2', 1000);
    // cache.put('key3', 'value1');
    let start = Date.now();
    for (let i = 0; i < countmax; i++) {
        size = cache.memsize();
    }
    let end = Date.now();
    print(`memory-cache_memsize: ${end - start} ms`);
    // print(size)
    if (size != 10) {
        throw new Error('memory-cache_memsize err');
    }
}

function exportJson_test() {
    let cache = new Cache()
    let exportJson;
    for(let i=0;i<10;i++){
        cache.put('key'+i, 'this is function', 100, function () { });
    }
    // cache.put('key1', 'this is function', 100, function () { });
    // cache.put('key2', 'value2', 1000);
    // cache.put('key3', 'value1');
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        exportJson = cache.exportJson();
    }
    let end = Date.now();
    print(`memory-cache_exportJson: ${end - start} ms`)
    if (exportJson.indexOf('this is function') == -1) {
        throw new Error('memory-cache_exportJson err')
    }
}
function get_test() {
    let cache = new Cache()
    let key;
    cache.put('key1', 'this is function', 100, function () { });
    cache.put('key2', 'value2', 1000);
    cache.put('key3', 'value1');
    let start = Date.now();
    for (let i = 0; i < countmax; i++) {
        key = cache.get('key2');
    }
    let end = Date.now();
    print(`memory-cache_get: ${end - start} ms`)
    // print(key)
    if (key != 'value2') {
        throw new Error('memory-cache_get err')
    }
}
function keys_test() {
    var cache1 = new Cache();
    let key;
    // cache1.put('key1', 'this is function', 100, function () { });
    // cache1.put('key2', 'value2', 1000);
    for(let i=0;i<10;i++){
        cache1.put('key'+i, 'this is function', 100, function () { });
    }
    let start = Date.now();
    for (let i = 0; i < countmax; i++) {
        key = cache1.keys();
    }
    let end = Date.now();
    print(`memory-cache_keys: ${end - start} ms`)
    // print(key,cache1.exportJson())
    if (key.length != 10) {
        throw new Error('memory-cache_keys err')
    }
}
function importJson_test() {
    let cache = new Cache();
    // cache.debug(true);
    cache.put('key1', 'value1', 1);
    cache.put('key2', 'value2', 1000);
    var exportedJson = cache.exportJson();
    // print(exportedJson)
    cache.clear();
    cache.put('key1', 'changed value', 5000);
    cache.put('key3', 'value3', 500);
    cache.put('key4', 'value4', 500);
    // print(cache.size())   //3
    let num;
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        num = cache.importJson(exportedJson, { skipDuplicates: true });
    }
    let end = Date.now();
    print(`memory-cache_importJson: ${end - start} ms`)
    if (num != 4) {
        throw new Error('memory-cache_importJson err')
    }
}
function hit_test() {
    let cache = new Cache();
    cache.debug(true)
    cache.put('key1', 'value1', 100);
    cache.put('key2', 'value2', 300);
    cache.put('key3', 'value3', 500);
    cache.get('key1');
    cache.get('key1');
    cache.get('key2');
    cache.get('key3');
    let num;
    let start = Date.now();
    for (let i = 0; i < 500000; i++) {
        num = cache.hits()
    }
    let end = Date.now();
    print(`memory-cache_hits: ${end - start} ms`)
    if (num != 4) {
        throw new Error('memory-cache_hits err')
    }
}
function misses_test() {
    let cache = new Cache();
    cache.debug(true)
    cache.put('key1', 'value1');
    cache.put('key2', 'value2');
    cache.put('key3', 'value3');
    cache.get('key1');
    cache.get('miss');
    cache.get('key3');
    let num;
    let start = Date.now();
    for (let i = 0; i < 500000; i++) {
        num = cache.misses()
    }
    let end = Date.now();
    print(`memory-cache_misses: ${end - start} ms`)
    if (num != 1) {
        throw new Error('memory-cache_misses err')
    }
}
//清除实际只执行一次，耗时太短
function clear_test() {
    let cache = new Cache()
    cache.put('key1', 'this is function', 100, function () { });
    cache.put('key2', 'value2', 1000);
    cache.put('key3', 'value1');
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        cache.clear();
    }
    let end = Date.now();
    print(`memory-cache_clear: ${end - start} ms`);
    if (cache.exportJson() != '{}') {
        throw new Error('memory-cache_clear err');
    }
}
function clearAddput_test() {
    let cache = new Cache()
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        cache.put('key1', 'this is function', 100, function () { });
        cache.put('key2', 'value2', 1000);
        cache.put('key3', 'value1');
        cache.clear();
    }
    let end = Date.now();
    print(`memory-cache_clearAddput: ${end - start} ms`);
    if (cache.exportJson() != '{}') {
        throw new Error('memory-cache_clearAddput err');
    }
}
function Addput_test() {
    let cache = new Cache()
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        cache.put('key1', 'this is function', 100, function () { });
        cache.put('key2', 'value2', 1000);
        cache.put('key3', 'value1');
    }
    let end = Date.now();
    print(`memory-cache_Addput: ${end - start} ms`);
    if (cache.size() != 3) {
        throw new Error('memory-cache_Addput err');
    }
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    put_test();
    // del_test();
    delAddPut_test();
    size_test();
    memsize_test();
    exportJson_test();
    get_test();
    keys_test();
    importJson_test();
    hit_test();
    misses_test();
    // clear_test()
    clearAddput_test()
    Addput_test()
}

ArkTools.waitAllJitCompileFinish();

await new Promise((resolve)=>{resolve();});
put_test();
// del_test();
delAddPut_test();
size_test();
memsize_test();
exportJson_test();
get_test();
keys_test();
importJson_test();
hit_test();
misses_test();
// clear_test()
clearAddput_test()
Addput_test()

